package com.example.controllers;


import com.baidu.aip.ocr.AipOcr;
import com.example.dao.UsersMapper;
import com.example.pojo.Users;
import com.example.tools.DataBase;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping
public class TestConltroller {

    @Autowired
    private UsersMapper usersMapper;

    @RequestMapping("/install")
    @ResponseBody
    public String install() throws FileNotFoundException {
        System.out.println(ResourceUtils.getURL("classpath:").getPath());
        DataBase.installDatabase(ResourceUtils.getURL("classpath:").getPath()+"seafood.sql", "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306", "root", "root");
        return "没报异常";
    }

    @RequestMapping("/user/fff")
    @ResponseBody
    public String rola(){
        return "有权限";
    }

    @RequestMapping("/fuck/haha")
    @ResponseBody
    public String rola1(){
        return "穆翔有权限";
    }


    @RequestMapping("/test")
    public String testPage(){
        return "helloPage";
    }

    @RequestMapping("/testexecl")
    public ResponseEntity<byte[]> test(HttpServletResponse  response) throws IOException {

        XSSFWorkbook workbook = new XSSFWorkbook();// excel
        XSSFSheet sheet = workbook.createSheet("testSheet");

        List<Users> list = usersMapper.selectAll();
        XSSFRow headrow = sheet.createRow(0);
        headrow.createCell(0).setCellValue("ID");
        headrow.createCell(1).setCellValue("用户名");
        headrow.createCell(2).setCellValue("密码");
        headrow.createCell(3).setCellValue("真实姓名");
        headrow.createCell(4).setCellValue("身份证号");
        headrow.createCell(5).setCellValue("电话");
        for(int i=0;i<list.size();i++) {
            Users user = list.get(i);
            XSSFRow row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(user.getUid());
            row.createCell(1).setCellValue(user.getUsername());
            row.createCell(2).setCellValue(user.getPassword());
            row.createCell(3).setCellValue(user.getRealname());
            row.createCell(4).setCellValue(user.getIdentitynumber());
            row.createCell(5).setCellValue(user.getTel());
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        workbook.write(baos);

        byte[] re = baos.toByteArray();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachment", "new.xlsx");
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(re, headers, HttpStatus.CREATED);
        return responseEntity;
    }

    @RequestMapping("/lg")
    public String userHello1(Map<String, Users> map, HttpSession session, String username, String password){

        UsernamePasswordToken upt = new UsernamePasswordToken(username, password);

        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(upt);

            return "redirect:hello.html";

        } catch (Exception e) {

            return "redirect:login.html";            // succss ="bad";
        }

    }


    @RequestMapping("/aip")
    @ResponseBody
    public String aipTest(){
        //设置APPID/AK/SK
        String APP_ID = "10332569";
        String API_KEY = "hS2Mg8TZ46YuFB9chO3kFegH";
        String SECRET_KEY = "H7y5T8QnVlFKB3VxnpYK4RoVnOnf76OA";

        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
       //  System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");

        // 调用接口
        String path = "test.jpg";
        System.out.println( new File(".").getAbsolutePath());
        JSONObject res = client.idcard(path, true,new HashMap<String, String>());
        String re = res.toString(2);
        System.out.println(re);

        return re;

    }



}
